//
//  ViewController.m
//  23-QQMusic
//
//  Created by dllo on 16/8/15.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "MvTableViewCell.h"
#import "RootModel.h"
#import "Contents.h"
#import "Data.h"
#import "List.h"
#import "MvList.h"
#define kTableViewReuse @"cellReuse"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) RootModel *rootModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self getData];
}

- (void)getData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *urlString = @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1";
    [manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *data = responseObject;
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        dataStr = [dataStr stringByReplacingOccurrencesOfString:@"jsonp1(" withString:@""];
        dataStr = [dataStr substringToIndex:dataStr.length - 1];
        NSData *newData = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:newData options:NSJSONReadingMutableContainers error:&error];
        self.rootModel = [RootModel modelWithDic:dic];
        [_myTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [_rootModel.data.contents.list[section] name];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _rootModel.data.contents.list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MvTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    cell.mvListArray = _rootModel.data.contents.list[indexPath.section].mvlist;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
